# 此脚本是用静态方式来生成扩展测试用例的初始条件和操作步骤部分
import re


def power_station_change(test_case):
    # 切换初始状态和操作步骤中的电源模式
    preconditions = test_case['preconditions']
    operate_step = test_case['operate_step']
    
    # 切换初始状态中的电源模式
    if isinstance(preconditions, list):
        preconditions = '\n'.join(preconditions)
    preconditions_lines = preconditions.splitlines()
    for line in preconditions_lines:
        if "电源" in line:
            for station in ['ON', 'OFF', 'START']:
                if station not in line:
                    continue
                
                for other_station in ['ON', 'OFF', 'START']:
                    if other_station != station:
                        new_line = line.replace(station, other_station)
                        new_preconditions = preconditions.replace(line, new_line)
                        
                        new_test_case = test_case.copy()
                        new_test_case['test_name'] = ''
                        new_test_case['preconditions'] = new_preconditions
                        new_test_case['operate_step'] = operate_step
                        new_test_case['expected_results'] = ''
                        new_test_case['test_level'] = '扩展'
                        
                        yield new_test_case
    
    # 切换操作步骤中的电源模式
    if isinstance(operate_step, list):
        operate_step = '\n'.join(operate_step)
    operate_step_lines = operate_step.splitlines()
    for line in operate_step_lines:
        if "电源模式" in line:
            for station in ['ON', 'OFF', 'START']:
                if station not in line:
                    continue
                
                for other_station in ['ON', 'OFF', 'START']:
                    if other_station != station:
                        new_line = line.replace(station, other_station)
                        new_operate_step = operate_step.replace(line, new_line)
                        
                        new_test_case = test_case.copy()
                        new_test_case['test_name'] = ''
                        new_test_case['preconditions'] = preconditions
                        new_test_case['operate_step'] = new_operate_step
                        new_test_case['expected_results'] = ''
                        new_test_case['test_level'] = '扩展'
                        
                        yield new_test_case


import re

def traverse_signal_values(test_case):
    # 遍历信号量的取值
    '''
    有四种信号量：
    INFOCAN: HU_WiperRainSensitivitySet的取值范围为1-7
    LIN信号RLS_LIN2:RLS_Wiper_speed的取值范围为1-14
    BCM_WiperRainSensitivity的取值范围是1-6
    BCM_Rain_sensitivity的取值范围是0-4

    逐行判断前置条件和操作步骤中是否包含这些信号量，如果包含则进行遍历替换，生成新的测试用例
    '''

    preconditions = test_case['preconditions']
    operate_step = test_case['operate_step']

    # 定义信号名到正则和取值范围的映射
    signals = {
        'HU_WiperRainSensitivitySet': (r'(HU_WiperRainSensitivitySet\b\s+)(\d+)', range(1, 8)),
        'RLS_Wiper_speed': (r'(RLS_Wiper_speed\b\s+)(\d+)', range(1, 15)),
        'BCM_WiperRainSensitivity': (r'(BCM_WiperRainSensitivity\b\s+)(\d+)', range(1, 7)),
        'BCM_Rain_sensitivity': (r'(BCM_Rain_sensitivity\b\s+)(\d+)', range(0, 5)),
    }

    # 处理前置条件中的信号量
    if isinstance(preconditions, list):
        preconditions = '\n'.join(preconditions)
    pre_lines = preconditions.splitlines()
    for line in pre_lines:
        for name, (pattern, value_range) in signals.items():
            if name not in line:
                continue
            matches = re.finditer(pattern, line)
            for m in matches:
                try:
                    current = int(m.group(2))
                except ValueError:
                    continue
                for v in value_range:
                    if v == current:
                        continue
                    # 仅替换该行中匹配的数字部分
                    new_line = line[:m.start(2)] + str(v) + line[m.end(2):]
                    new_preconditions = preconditions.replace(line, new_line, 1)
                    new_test_case = test_case.copy()
                    new_test_case['test_name'] = ''
                    new_test_case['preconditions'] = new_preconditions
                    new_test_case['operate_step'] = operate_step
                    new_test_case['expected_results'] = ''
                    new_test_case['test_level'] = '扩展'
                    yield new_test_case

    # 处理操作步骤中的信号量
    if isinstance(operate_step, list):
        operate_step = '\n'.join(operate_step)
    op_lines = operate_step.splitlines()
    for line in op_lines:
        for name, (pattern, value_range) in signals.items():
            if name not in line:
                continue
            matches = re.finditer(pattern, line)
            for m in matches:
                try:
                    current = int(m.group(2))
                except ValueError:
                    continue
                for v in value_range:
                    if v == current:
                        continue
                    new_line = line[:m.start(2)] + str(v) + line[m.end(2):]
                    new_operate_step = operate_step.replace(line, new_line, 1)
                    new_test_case = test_case.copy()
                    new_test_case['test_name'] = ''
                    new_test_case['preconditions'] = preconditions
                    new_test_case['operate_step'] = new_operate_step
                    new_test_case['expected_results'] = ''
                    new_test_case['test_level'] = '扩展'
                    yield new_test_case

def switch_state_negated(test_case):
    # 切换初始状态和操作步骤中的状态取反
    preconditions = test_case['preconditions']
    operate_step = test_case['operate_step']
    
    negation_map = {
        '打开': '关闭',
        '关闭': '打开',
        '启动': '停止',
        '停止': '启动',
        '断开': '闭合',
        '闭合': '断开',
        '有效': '无效',
        '无效': '有效',
    }
    signal_list = ['J5-50', 'J5-49', 'J2-32', 'J2-17', 'J2-72', 'J3-24']
    
    # 切换初始状态中的状态取反
    if isinstance(preconditions, list):
        preconditions = '\n'.join(preconditions)
    preconditions_lines = preconditions.splitlines()
    for line in preconditions_lines:
        for state, negated in negation_map.items():
            # 这一句中只包含 signal_list 中的信号量，且只包含一个信号量，多个的话就不处理
            if not any(signal in line for signal in signal_list):
                continue
            if sum(1 for signal in signal_list if signal in line) > 1:
                continue

            if state in line:
                new_line = line.replace(state, negated)
                if '高电平' in new_line:
                    new_line = new_line.replace('高电平', '低电平')
                elif '低电平' in new_line:
                    new_line = new_line.replace('低电平', '高电平')
                else:
                    continue
                new_preconditions = preconditions.replace(line, new_line)
                new_test_case = test_case.copy()
                new_test_case['test_name'] = ''
                new_test_case['preconditions'] = new_preconditions
                new_test_case['operate_step'] = operate_step
                new_test_case['expected_results'] = ''
                new_test_case['test_level'] = '扩展'
                
                yield new_test_case
    
